/****** MP3_Player.c ********************************************
 * BRUR 
 * Tyler Fultz 
 * Carter Seward
 * Use of onboard MP3 Codec to play MP3 files on MIKRO BOARD
 *
 **********************************************************************/
/*MikroBoard Hardware Includes*/
#include "p24FJ256GB110.h"       // PIC24 register and bit definitions
/*C Library Includes*/
#include "string.h"
/*MikroBoard Functionality*/
#include "Mikro.h"               // LCD variables, functions, macros
#include "MikroTouch.h"		 // This implement touch screen on LCD
#include "MikroSPI.h"            // SPI Functionality for MikroBoard
/*Program Includes*/
#include "MikroMP3.h"
//#include "MikroSD.h"
#include "MikroFlash.h"

#include "MikroDebug.c"

/****** Configuration selections **************************************/
_CONFIG1(JTAGEN_OFF & GWRP_OFF & FWDTEN_OFF & ICS_PGx2);
_CONFIG2(PLLDIV_DIV2 & POSCMOD_HS & FNOSC_PRIPLL & IOL1WAY_OFF);

/****** Macros ********************************************************/
#define NumCharsDisplay (9)

/****** Global variables **********************************************/

unsigned int ALIVECNT = 99;

int pick_num = -1;
long int display_num = 0;

int numChar;



/****** Function prototypes *******************************************/
void Initial(void);
void BlinkAlive(void);

int NumQue = 0; //number of words waiting to play


//////// Main Function /////////////////////////////////////////////////

uint8 SendInt[] = {0xAA,0x55};
uint8 RcvInt[2];

int main() {
    Initial(); // Make a call to the Initial Function to set everything up

    M25P80_sector_erase(0);
    M25P80_page_program( 0, 2, SendInt);
    M25P80_read(0,2,RcvInt);

    DisplayByte(1, SendInt[0]);
    DisplayByte(2, SendInt[1]);
    DisplayByte(3, RcvInt[0]);
    DisplayByte(4, RcvInt[1]);

    while (1) {
        BlinkAlive();   // Call Blink Alive Function
        DetectTouch();  // Detects touch positions on the screen
        
        while(!_T5IF);
	   _T5IF = 0;
    }

}

/****** Initial ********************************************************
 *
 * Initialize LCD Screen (PMP + configuration + initial display).
 * Initialize Timer5 for a loop time of 10 ms.
 * Initialize display string for each line of MCR information.
 * Enable remappable pin RP1 into INT2 external interrupt and enable interrupt.
 *
 **********************************************************************/
void Initial() {
    AD1PCFGL = 0xFFFF; // Make all ADC pins default to digital pins
    PMP_Init(); // Init PMP module for LCD
    LCD_Init(); // Config LCD
    InitBackground();

    _TRISA3 = 0; // Make RA3 an output (pin 50 of Mikro board)

    TMR5 = 0; // Clear Timer5
    PR5 = 19999; // Set period of Timer5 to 10 ms
    T5CON = 0x8010; // Clock Timer5 with Fcy/8 = 2 MHz

    /*Initial Functions*/
    SPI2_Init();
    M25P80_Init();
}

/****** BlinkAlive *****************************************************
 *
 * This function toggles a square of pixels every second.
 * With a loop time of 10 ms, this is every 100 looptimes.
 **********************************************************************/
void BlinkAlive() {
    ALIVECNT++;
    if (ALIVECNT == 100) // Write black square
    {
        DrawRectangle(0, 0, 5, 5, BLACK);
        //SCI_Spi2Write(SCI_VOL_ADDR, 0x0);

    }
    if (ALIVECNT >= 200) // Clear black square
    {
        ALIVECNT = 0;
        DrawRectangle(0, 0, 5, 5, BKGD);
        //SCI_Spi2Write(SCI_VOL_ADDR, 0xFFFF);

    }
}

